package JVM;

import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author:谢君臣
 * @Date:2021/8/30-21:03
 * @version:1.0
 * @function:
 *  -Xmx20m -XX:+PrintGCDetails -verbose:gc
 */
public class 软引用 {
    private static int _MB = 4*1024*1024;
    public static void main(String[] args) {
        List<SoftReference<byte[]>> list = new ArrayList<>();
        ReferenceQueue<byte[]> queue = new ReferenceQueue<>();//引用队列
        for (int i = 0; i < 5; i++) {
            SoftReference<byte[]> ref = new SoftReference<>(new byte[_MB],queue);
            System.out.println(ref.get());
            list.add(ref);
        }
        Reference<? extends byte[]> poll = queue.poll();
        while (poll!=null){
            list.remove(poll);
            poll = queue.poll();
        }
        for (SoftReference<byte[]> softReference : list) {
            System.out.println(softReference.get());
        }
    }
}
